package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车数据
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //1. 拷贝对象数据
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());

        //2. 查询套餐
        /**
         * userid : 1 dishId: 110 select * from shopping_cart where user_id = 1 and dish_id = 110
         *
         * userid : 1setmealId: 120 select * from shopping_cart where user_id = 1 and setmeal_id = 120
         *
         * 同样一个用户对同一个菜品或者套餐下多份数据库中只存储一行记录, 如果同一个用户即购买菜品又购买了套餐数据库存储多行记录
         */
        List<ShoppingCart> list = shoppingCartMapper.list(cart);

        //3. 判断当前是需要新增还是修改
        if(null != list && list.size() == 1){
            //4. 如果是修改直接修改分数即可
            ShoppingCart myCart = list.get(0);
            myCart.setNumber(myCart.getNumber() + 1);

            /*if(null!= shoppingCartDTO.getDishFlavor() && !myCart.getDishFlavor().equals(shoppingCartDTO.getDishFlavor())){
                myCart.setDishFlavor(myCart.getDishFlavor() + "," + shoppingCartDTO.getDishFlavor());
            }*/

            shoppingCartMapper.update(myCart);
        }else{
            //5. 如果是新增

            if(null != shoppingCartDTO.getDishId()){
                //5.1 新增菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
                cart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            }else {
                //5.2 新增套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());

                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }

            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(cart);
        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart cart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();

        return shoppingCartMapper.list(cart);
    }

    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }
}
